set_project(usbtool)

add_rules("mode.release", "mode.debug" , "mode.releasedbg" , "mode.minsizerel")

rule("copy_as_text")
    on_buildcmd_file(function (target, batchcmds, sourcefile, opt)
        -- add objectfile
        local objectfile = target:objectfile(sourcefile)
        table.insert(target:objectfiles(), objectfile)

        local gcc = target:tool("cc")
        local objpath , _ = path.filename(gcc):gsub("gcc", "objcopy")
        local objcopy = path.join(path.directory(gcc), objpath)

        local arch = "arm"
        local bit = target:check_sizeof("void *")
        bit = bit * 8
        if get_config('arch') == "arm64" then
            arch = "aarch64"
        end

        -- add commands
        batchcmds:mkdir(path.directory(objectfile))
        batchcmds:vrunv(objcopy, {"-I" , "binary" ,"-O" ,"elf".. bit .."-little" .. arch, "-B", arch , sourcefile, objectfile})
        -- show build progress
        batchcmds:show_progress(opt.progress, "objcopy %s -> %s", sourcefile , objectfile)
        -- add deps
        batchcmds:add_depfiles(sourcefile)
        batchcmds:set_depmtime(os.mtime(objectfile))
        batchcmds:set_depcache(target:dependfile(objectfile))
    end)

toolchain("msvc-wine")
    -- mark as standalone toolchain
    set_kind("standalone")
    on_load(function (toolchain)
        toolchain:set("toolset", "cc",  "cl.exe")
        toolchain:set("toolset", "cxx", "cl.exe")
        toolchain:set("toolset", "mrc", "rc.exe")
        if toolchain:is_arch("x86") then
            toolchain:set("toolset", "as",  "ml.exe")
        elseif toolchain:is_arch("arm64") then
            toolchain:set("toolset", "as",  "armasm64_msvc@armasm64.exe")
        elseif toolchain:is_arch("arm.*") then
            toolchain:set("toolset", "as",  "armasm_msvc@armasm.exe")
        else
            toolchain:set("toolset", "as",  "ml64.exe")
        end
        toolchain:set("toolset", "ld",  "link.exe")
        toolchain:set("toolset", "sh",  "link.exe")
        toolchain:set("toolset", "ar",  "link.exe")
    end)
    add_cxflags("-MD")
    add_cxflags("RTC1")
toolchain_end()

if is_plat("windows") then
    local basedir = path.join("third_package" , "pthread" , "windows")

    add_cxflags("/utf-8")
    add_cxflags("/DWIN32")
    add_cxflags("/D_WINDOWS")
    add_includedirs(basedir)
    add_cxflags("-DHAVE_STRUCT_TIMESPEC")
    add_cxflags("-D_WINSOCK_DEPRECATED_NO_WARNINGS")

    if get_config("arch") == "x64" then
        add_linkdirs(path.join(basedir , "x64"))
        add_installfiles(path.join(basedir , "x64" , "pthread.dll") , {prefixdir = "bin"})
        add_installfiles(path.join(basedir , "x64" , "pthread.dll") , {prefixdir = "dev_helper"})
    else
        add_linkdirs(path.join(basedir , "win32"))
        add_installfiles(path.join(basedir , "win32" , "pthread.dll") , {prefixdir = "bin"})
        add_installfiles(path.join(basedir , "win32" , "pthread.dll") , {prefixdir = "dev_helper"})
    end
end

option("USING_8030USB")
    set_default(true)

option("USING_8030SDIO")
    set_default(false)

option("BUILD_TEST_APP")
    set_default(true)

option("ENABLE_PYTHON")
    set_default(false)

option("ENABLE_JAVA")
    set_default(false)

option("BUILD_RAM_INIT")
    set_default(false)

option("BUILD_TUNTAP")
    set_default(true)

option("APP_STATIC_LIB")
    set_default(false)

option("BUILD_ARTOSYN_EXAMPLE")
    set_default(false)

option("BUILD_BW_UPDATE_DEMO")
    set_default(false)

option("BUILD_IMG_UPGRADE")
    set_default(false)

option("BUILD_XDATA_TEST")
    set_default(false)

option("BUILD_WORK_MODE_CFG")
    set_default(false)

option("BUILD_BB_TEST")
    set_default(false)

option("JAVA_HOME")
    set_description("set JAVA_HOME")

includes("third_package")
includes("third_package/from-repo.lua")
includes("com")
includes("app")
includes("daemon")
includes("dev_helper")
